/*
 * usbasp.c - part of USBasp
 *
 * Autor..........: Thomas Fischl <tfischl@gmx.de>
 * Description....: Definitions and macros for usbasp
 * Licence........: GNU GPL v2 (see Readme.txt)
 * Creation Date..: 2009-02-28
 * Last change....: 2009-02-28
 */

#ifndef USBASP_H_
#define USBASP_H_

/* USB function call identifiers */
#define USBASP_FUNC_CONNECT     1
#define USBASP_FUNC_DISCONNECT  2
#define USBASP_FUNC_TRANSMIT    3
#define USBASP_FUNC_READFLASH   4
#define USBASP_FUNC_ENABLEPROG  5
#define USBASP_FUNC_WRITEFLASH  6
#define USBASP_FUNC_READEEPROM  7
#define USBASP_FUNC_WRITEEEPROM 8
#define USBASP_FUNC_SETLONGADDRESS 9
#define USBASP_FUNC_SETISPSCK 10

/* programming state */
#define PROG_STATE_IDLE         0
#define PROG_STATE_WRITEFLASH   1
#define PROG_STATE_READFLASH    2
#define PROG_STATE_READEEPROM   3
#define PROG_STATE_WRITEEEPROM  4

/* Block mode flags */
#define PROG_BLOCKFLAG_FIRST    1
#define PROG_BLOCKFLAG_LAST     2

/* ISP SCK speed identifiers */
#define USBASP_ISP_SCK_AUTO   0
#define USBASP_ISP_SCK_0_5    1   /* 500 Hz */
#define USBASP_ISP_SCK_1      2   /*   1 kHz */
#define USBASP_ISP_SCK_2      3   /*   2 kHz */
#define USBASP_ISP_SCK_4      4   /*   4 kHz */
#define USBASP_ISP_SCK_8      5   /*   8 kHz */
#define USBASP_ISP_SCK_16     6   /*  16 kHz */
#define USBASP_ISP_SCK_32     7   /*  32 kHz */
#define USBASP_ISP_SCK_93_75  8   /*  93.75 kHz */
#define USBASP_ISP_SCK_187_5  9   /* 187.5  kHz */
#define USBASP_ISP_SCK_375    10  /* 375 kHz   */
#define USBASP_ISP_SCK_750    11  /* 750 kHz   */
#define USBASP_ISP_SCK_1500   12  /* 1.5 MHz   */

/* macros for gpio functions */
//#define ledRedOn()    PORTC &= ~(1 << PC1)
//#define ledRedOff()   PORTC |= (1 << PC1)
//#define ledGreenOn()  PORTC &= ~(1 << PC0)
//#define ledGreenOff() PORTC |= (1 << PC0)

/* Always on, when powered */
#define LED_R			/* If this define, led will work */
#define LED_RED			PA0
#define LED_RED_DDR		DDRA
#define LED_RED_PORT	PORTA

/* On, while programming */
#define LED_G			/* If this define, led will work */
#define LED_GREEN		PB0
#define LED_GREEN_DDR	DDRB
#define LED_GREEN_PORT	PORTB

#ifdef	LED_G
#define ledGreenOn()	sbi(LED_GREEN_PORT,LED_GREEN)
#define ledGreenOff()	cbi(LED_GREEN_PORT,LED_GREEN)
#define ledGreenInit()	\
{\
	sbi(LED_GREEN_DDR,LED_GREEN);\
	ledGreenOn();\
}
#else
#define ledGreenInit()	{}
#define ledGreenOn()	{}
#define ledGreenOff()	{}
#endif

#ifdef	LED_R
#define ledRedOn()		sbi(LED_RED_PORT,LED_RED)
#define ledRedOff()		cbi(LED_RED_PORT,LED_RED)
#define ledRedInit()	\
{\
	sbi(LED_RED_DDR,LED_RED);\
	ledRedOff();\
}
#else
#define ledRedInit()	{}
#define ledRedOn()		{}
#define ledRedOff()		{}
#endif

#endif /* USBASP_H_ */
